package com.exercise.module.common.file.service;/**
 * Created by kete on 2017-05-11.
 */

import com.exercise.module.common.config.service.ConfigService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

/**
 * 作者：kete
 * 创建时间：2017-05-11 16:29
 * 功能描述：
 * 版本：
 */
public abstract class AbstractFileService implements FileService {

    private static Logger logger = LoggerFactory.getLogger(AbstractFileService.class);

    @Autowired
    private ConfigService configService;

    public static String separator = "/";

    public String getLocationDir() {


        String locationDir = configService.getString("location_dir");

        if (StringUtils.isEmpty(locationDir)) {
            locationDir = System.getProperty("user.home") + separator + ".ebidding/uploadDir";
        } else {
            File tempDir = new File(locationDir);
            if (!tempDir.exists()) {
                locationDir = System.getProperty("user.home") + separator + ".ebidding/uploadDir";
            }
        }

        locationDir = locationDir.replaceAll("\\\\", separator);
        return locationDir;
    }

    public String getTempDir(String modules1, String modules2) {

        String locationTempDir = configService.getString("location_temp_dir");

        if (StringUtils.isEmpty(locationTempDir)) {
            locationTempDir = System.getProperty("user.home") + separator + ".ebidding/temp";
        } else {
            File tempDir = new File(locationTempDir);
            if (!tempDir.exists()) {
                locationTempDir = System.getProperty("user.home") + separator + ".ebidding/temp";
            }
        }

        String companyCode = getCompanyCode();
        String dateStr = DateFormatUtils.format(new Date(), "yyyy-MM-dd");

        String dir = locationTempDir + separator;
        dir += companyCode + separator;
        dir += dateStr + separator;
        dir += modules1 + separator;
        dir += modules2 + separator;
        dir += getUUID();
        mkdirs(dir);
        dir = StringUtils.replace(dir, "\\", separator);
        return dir;
    }

    public String getTempFile(String modules1, String modules2, String ext) {

        String locationTempDir = configService.getString("location_temp_dir");

        if (StringUtils.isEmpty(locationTempDir)) {
            locationTempDir = System.getProperty("user.home") + separator + ".ebidding/temp";
        } else {
            File tempDir = new File(locationTempDir);
            if (!tempDir.exists()) {
                locationTempDir = System.getProperty("user.home") + separator + ".ebidding/temp";
            }
        }

        String companyCode = getCompanyCode();
        String dateStr = DateFormatUtils.format(new Date(), "yyyy-MM-dd");

        String dir = locationTempDir + separator;
        dir += companyCode + separator;
        dir += dateStr + separator;
        dir += modules1 + separator;
        dir += modules2 + separator;
        mkdirs(dir);

        String fileName = dir += getUUID() + "." + ext;
        fileName = StringUtils.replace(fileName, "\\", separator);
        return fileName;
    }


    public String getUUID() {

        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }

    public String getCompanyCode() {

        String companyCode = "onelinkplus";
        return companyCode;
    }

    public void mkdirs(String dir) {

        File fileDir = new File(dir);
        if (!fileDir.exists()) {
            boolean isMk = fileDir.mkdirs();
            if (!isMk) {
                logger.warn("create error");
            }
        }
    }

    public String createChecksum(InputStream input) {
        return "";
//		try {
//			input.skip(0);
//			ExecTime execTime = new ExecTime();
//
//			byte[] buffer = new byte[1024 * 2];
//			MessageDigest complete = MessageDigest.getInstance("SHA1");
//			int numRead;
//
//			do {
//				numRead = input.read(buffer);
//				if (numRead > 0) {
//					complete.update(buffer, 0, numRead);
//				}
//			} while (numRead != -1);
//
//			byte[] byteArray = complete.digest();
//			StringBuffer strBuff = new StringBuffer();
//
//	        for (int i = 0; i < byteArray.length; i++) {
//	            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
//	            	strBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
//	            else
//	            	strBuff.append(Integer.toHexString(0xFF & byteArray[i]));
//	        }
//
//	        logger.info(execTime.toString());
//	        return strBuff.toString();
//		} catch(Exception e) {
//			e.printStackTrace();
//			return "";
//		} finally {
//			IOUtils.closeQuietly(input);
//		}
    }
}
